<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model.trim="username" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model.number="age" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="sex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click.prevent="jia">添加/修改</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item, index) in arr" :key="item.id">
          <td>{{ index + 1 }}</td>
          <td>{{ item.username }}</td>
          <td>{{ item.age }}</td>
          <td>{{ item.sex }}</td>
          <td>
            <button @click.prevent="del(item.id)">删除</button>
            <button @click.prevent="ji">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        { id: 15, username: "小李", age: 89, sex: "男" },
        { id: 16, username: "小望", age: 59, sex: "男" },
      ],
      username: "",
      age: "",
      sex: "", 
    };
  },
  methods: {
    //删除
    del(id) {
      this.arr = this.arr.filter((item) => item.id != id);
    },
    //添加
    jia() {
      this.arr.push({
        id: +new Date(),
        username: this.username,
        age: this.age,
        sex: this.sex,
      });
      this.username = "";
      this.age = "";
      this.sex = "";
    },
    //编辑
    ji() {
      console.log(1);
    
    },
  },
};
</script>

<style></style>
